package com.lsl.endworkvol.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lsl.endworkvol.dao.ActivityMapper;
import com.lsl.endworkvol.dao.SignUpNoteMapper;
import com.lsl.endworkvol.entity.Activity;
import com.lsl.endworkvol.entity.SignUpNote;
import com.lsl.endworkvol.service.ActivityService;
import com.lsl.endworkvol.service.ActivitySignUpService;
import com.lsl.endworkvol.service.func.ISignUpNoteBuilder;

@Service
public class ActivitySignUpServiceImpl implements ActivitySignUpService {

    @Autowired
    private SignUpNoteMapper signUpNoteMapper;

    @Autowired
    private ActivityMapper activityMapper;

    @Override
    public SignUpNote getNote(Integer id) {
        return signUpNoteMapper.selectById(id);
    }

    @Override
    public List<SignUpNote> getNotes(ISignUpNoteBuilder builder) {
        SignUpNote temp = builder.build();
        if(temp.getScore() != null || temp.getCheckOutFlag() != null) {
            /// 模糊查询
            return signUpNoteMapper.blur(temp);
        }
        if (temp.getActivityId() != null && temp.getVolunteerId() == null) {
            /// 根据活动id查询
            return signUpNoteMapper.selectByActivityId(temp.getActivityId());
        } else if (temp.getActivityId() == null && temp.getVolunteerId() != null) {
            /// 根据志愿者的用户id查询
            return signUpNoteMapper.selectByVolunteerId(temp.getVolunteerId());
        } else if (temp.getActivityId() != null && temp.getVolunteerId() != null) {
            /// 根据活动id和志愿者id查询
            return signUpNoteMapper.selectByUserIdAndActivityId(temp);
        } else {
            /// 错误的查询输入
            System.out.println("[ActivitySignUpServiceImpl] +++++ 错误的 getNotes 查询输入");
            return null;
        }

    }

    @Override
    public boolean signupActivity(ISignUpNoteBuilder builder) {
        boolean flag = signUpNoteMapper.insert(builder.build()) > 0;
        if (flag) {
            int activityId = builder.build().getActivityId();
            if(activityMapper.updateVolunteerCountPlus(activityId) > 0) {
                return flag;
            } else {
                return false;
            }
        }
        return false;
    }

    @Override
    public boolean exitActivity(String id) {
        // SignUpNote temp = getNote(Integer.parseInt(id));
        // Activity tempActivity = activityService.getActivity(String.valueOf(temp.getActivityId()));
        // Date dateNow = new Date();
        // if(dateNow.after(tempActivity.getSignUpEndTime())) {
        //     return false;
        // }
        // return signUpNoteMapper.delete(Integer.parseInt(id)) > 0;
        int activityId = getNote(Integer.parseInt(id)).getActivityId();
        boolean flag = signUpNoteMapper.delete(Integer.parseInt(id)) > 0;
            //int activityId = getNote(Integer.parseInt(id)).getActivityId();
        if (flag) {
            if(activityMapper.updateVolunteerCountMinus(activityId) > 0) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    @Override
    public boolean checkOutActivity(String id) {
        SignUpNote temp = getNote(Integer.parseInt(id));
        // Activity tempActivity = activityService.getActivity(String.valueOf(temp.getActivityId()));
        // Date dateNow = new Date();
        // if(dateNow.after(tempActivity.getEndTime())) {
        //     return false;
        // }
        temp.setCheckOutFlag(1);
        return signUpNoteMapper.update(temp) > 0;
    }

}
